package com.ybkj.daijia.server.controller;

import com.ybkj.daijia.serverUtils.HttpClientUtil;
import com.ybkj.daijia.Setting;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.WxSetting;
import com.ybkj.daijia.api.weixin.WeixinUtil;
import com.ybkj.daijia.common.Pager;
import com.ybkj.daijia.common.Principal;
import com.ybkj.daijia.common.Result;
import com.ybkj.daijia.server.exception.NoPermissionException;
import com.ybkj.daijia.page.Page;
import com.ybkj.daijia.server.event.model.OperationLogEvent;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.WeixinManager;
import com.ybkj.daijia.server.model.WeixinManagerSetting;
import com.ybkj.daijia.server.model.WeixinManagerTixianRecord;
import com.ybkj.daijia.server.service.AreaService;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.WeixinManagerService;
import com.ybkj.daijia.server.service.WeixinManagerSettingService;
import com.ybkj.daijia.server.service.WeixinManagerTixianRecordService;
import com.ybkj.daijia.server.service.WeixinManagerXOrderService;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author Shine
 */
@Controller
@RequestMapping(value = "weixinmanager")
public class WeixinManagerController {

    @Autowired
    private WeixinManagerService weixinManagerService;

    @Autowired
    private WeixinManagerTixianRecordService weixinManagerTixianRecordService;

    @Autowired
    private WeixinManagerXOrderService weixinManagerXOrderService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private CompanyService companyService;

    @Autowired
    private WeixinManagerSettingService weixinManagerSettingService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private ApplicationEventMulticaster applicationEventMulticaster;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView list(String phone, String name, String placeName, String idCard,
        Long companyId, Integer virtualType) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("weixin")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("weixinmanager/list");

            Page<WeixinManager> page = weixinManagerService
                .queryAll(phone, name, placeName, idCard, companyId, virtualType);
            for (WeixinManager weixinManager : page) {
//				weixinManager.setWaitTixian(weixinManagerTixianRecordService.countNewByManager(weixinManager.getId()));
                weixinManager.setCompany(companyService.findOne(weixinManager.getCompanyId()));
            }
            mv.addObject("page", page);
            mv.addObject("companys", companyService.findAll());

            mv.addObject("phone", phone);
            mv.addObject("name", name);
            mv.addObject("placeName", placeName);
            mv.addObject("idCard", idCard);
            mv.addObject("companyId", companyId);
            mv.addObject("virtualType", virtualType);

            return mv;
        } else {
            throw new NoPermissionException();
        }


    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView add() {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("weixin")) {
            ModelAndView mv = new ModelAndView();

            mv.addObject("areas", areaService.listAll());

            Company company = companyService.findOne(principal.getCompanyId());

            mv.addObject("currentProvName", company.getProvName());
            mv.addObject("currentCityName", company.getCityName());
            mv.addObject("companys", companyService.findAll());
            mv.addObject("areas", areaService.listAll());

            mv.setViewName("weixinmanager/add");

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    public @ResponseBody
    Result<WeixinManager> create(
        WeixinManager weixinManager) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        Result<WeixinManager> result = new Result<WeixinManager>();

        if (subject.isPermitted("weixin")) {

            if (phoneExists(weixinManager.getPhone(), null)) {
                result.setMessage("该电话号已存在");
                return result;
            }
            if (idCardExists(weixinManager.getPhone(), null)) {
                result.setMessage("该身份证号已存在");
                return result;
            }

            if (!principal.isCompanyType()) {
                weixinManager.setCompanyId(principal.getCompanyId());
            }

            weixinManager.setWxuser(null);
            weixinManager.setVirtual(BigDecimal.ZERO);
            weixinManager.setStatus(WeixinManager.WEIXINMANAGER_STATUS_OK);
            weixinManagerService.insertRecord(weixinManager);

            result.setSuccess(true);
            return result;

        } else {
            result.setMessage("您没有该权限");
            return result;
        }
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Long id) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("weixin")) {
            ModelAndView mv = new ModelAndView();

            WeixinManager weixinManager = weixinManagerService.findOne(id);

            mv.addObject("weixinManager", weixinManager);
            mv.addObject("currentProvName", weixinManager.getProvName());
            mv.addObject("currentCityName", weixinManager.getCityName());
            mv.addObject("companys", companyService.findAll());
            mv.addObject("areas", areaService.listAll());

            mv.setViewName("weixinmanager/edit");

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public @ResponseBody
    Result<WeixinManager> update(WeixinManager weixinManager) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("weixin")) {
            Result<WeixinManager> result = new Result<WeixinManager>();

            WeixinManager db = weixinManagerService.findOne(weixinManager.getId());

            if (phoneExists(weixinManager.getPhone(), weixinManager.getId())) {
                result.setMessage("该电话号已存在");
                return result;
            }
            if (idCardExists(weixinManager.getIdCard(), weixinManager.getId())) {
                result.setMessage("该身份证号已存在");
                return result;
            }

            if (principal.isCompanyType()) {

                if (!weixinManager.getCompanyId().equals(db.getCompanyId())
                    && db.getVirtual().doubleValue() != 0) {
                    result.setSuccess(false);
                    result.setMessage("余额不为0，不能修改公司");
                    return result;
                }

                db.setCompanyId(weixinManager.getCompanyId());
            }

            db.setLongitude(weixinManager.getLongitude());
            db.setLatitude(weixinManager.getLatitude());
            db.setProvName(weixinManager.getProvName());
            db.setCityName(weixinManager.getCityName());
            db.setPlace(weixinManager.getPlace());

            db.setName(weixinManager.getName());
            db.setPhone(weixinManager.getPhone());
            db.setIdCard(weixinManager.getIdCard());
            db.setWxAccount(weixinManager.getWxAccount());
            db.setPlaceName(weixinManager.getPlaceName());
            db.setAreaId(weixinManager.getAreaId());
            db.setPersonalInfo(weixinManager.getPersonalInfo());

            db.setFixedMoney(weixinManager.getFixedMoney());
            db.setRatioMoney(weixinManager.getRatioMoney());
            db.setWeixinManagerMoneyType(weixinManager.getWeixinManagerMoneyType());
            db.setFeeAllocation(weixinManager.getFeeAllocation());

            weixinManagerService.updateByPrimaryKey(db);

            String detail = "【%s】修改了客户经理【" + db.getName() + "】";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            result.setSuccess(true);
            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "audit/{id}", method = RequestMethod.GET)
    public ModelAndView audit(@PathVariable("id") Long id) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("weixin")) {
            ModelAndView mv = new ModelAndView();

            WeixinManager weixinManager = weixinManagerService.findOne(id);

            mv.addObject("weixinManager", weixinManager);
            mv.addObject("currentProvName", weixinManager.getProvName());
            mv.addObject("currentCityName", weixinManager.getCityName());
            mv.addObject("companys", companyService.findAll());
            mv.addObject("areas", areaService.listAll());

            mv.setViewName("weixinmanager/audit");

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "agreement", method = RequestMethod.GET)
    public ModelAndView agreement() {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("weixin")) {
            ModelAndView mv = new ModelAndView();

            mv.addObject("setting", weixinManagerSettingService.findOne());
            mv.addObject("username", principal.getUsername());

            mv.setViewName("weixinmanager/agreement");

            return mv;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "saveSetting", method = RequestMethod.POST)
    public String saveSetting(WeixinManagerSetting setting) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("weixin")) {

            WeixinManagerSetting db = weixinManagerSettingService.findOne();

            db.setAgreement(setting.getAgreement());

            String userName = principal.getUsername();
            if (userName.equals("shine")) {
                db.setTemplateAuditFirst(setting.getTemplateAuditFirst());
                db.setTemplateAuditRemark(setting.getTemplateAuditRemark());
            }

            weixinManagerSettingService.updateByPrimaryKey(db);

            String detail = "【%s】修改了客户经理合作协议";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            return "redirect:/weixinmanager/agreement";
        } else {
            throw new NoPermissionException();
        }

    }


    @RequestMapping(value = "audit", method = RequestMethod.POST)
    public @ResponseBody
    Result<WeixinManager> audit(WeixinManager weixinManager) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("weixin")) {
            Result<WeixinManager> result = new Result<WeixinManager>();

            WeixinManager db = weixinManagerService.findOne(weixinManager.getId());

            if (!db.getStatus().equals(WeixinManager.WEIXINMANAGER_STATUS_NOTYET)) {
                result.setSuccess(false);
                result.setMessage("该客户经理已经审核过");
                return result;
            }

            if (weixinManager.getStatus().equals(WeixinManager.WEIXINMANAGER_STATUS_OK)) {
                if (phoneExists(weixinManager.getPhone(), weixinManager.getId())) {
                    result.setMessage("该电话号已存在");
                    return result;
                }
                if (idCardExists(weixinManager.getPhone(), weixinManager.getId())) {
                    result.setMessage("该身份证号已存在");
                    return result;
                }

                if (principal.isCompanyType()) {
                    db.setCompanyId(weixinManager.getCompanyId());
                } else {
                    db.setCompanyId(principal.getCompanyId());
                }

                db.setLongitude(weixinManager.getLongitude());
                db.setLatitude(weixinManager.getLatitude());
                db.setProvName(weixinManager.getProvName());
                db.setCityName(weixinManager.getCityName());
                db.setPlace(weixinManager.getPlace());

                db.setName(weixinManager.getName());
                db.setPhone(weixinManager.getPhone());
                db.setIdCard(weixinManager.getIdCard());
                db.setWxAccount(weixinManager.getWxAccount());
                db.setPlaceName(weixinManager.getPlaceName());
                db.setAreaId(weixinManager.getAreaId());
                db.setPersonalInfo(weixinManager.getPersonalInfo());

                db.setFixedMoney(weixinManager.getFixedMoney());
                db.setRatioMoney(weixinManager.getRatioMoney());
                db.setWeixinManagerMoneyType(weixinManager.getWeixinManagerMoneyType());
                db.setFeeAllocation(weixinManager.getFeeAllocation());

                db.setStatus(WeixinManager.WEIXINMANAGER_STATUS_OK);
                db.setAuditMemo(weixinManager.getAuditMemo());
            } else if (weixinManager.getStatus()
                .equals(WeixinManager.WEIXINMANAGER_STATUS_FAILURE)) {
                db.setStatus(WeixinManager.WEIXINMANAGER_STATUS_FAILURE);
                db.setAuditMemo(weixinManager.getAuditMemo());
            }

            weixinManagerService.updateByPrimaryKey(db);

            String detail =
                "【%s】审核了客户经理【" + db.getName() + "】：" + weixinManager.getAuditMemo() + "";
            OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
            applicationEventMulticaster.multicastEvent(operationLogEvent);

            try {
                Setting setting = settingUtils.get();
                WxSetting wxSetting = settingUtils.getWx();
                WeixinManagerSetting weixinManagerSetting = weixinManagerSettingService.findOne();
                StringBuilder sb_url = new StringBuilder();
                sb_url.append(WeixinUtil.WX_URL).append("/wxmanager/").append(setting.getSecKey())
                    .append("?appId=").append(wxSetting.getWxAppId());
                String redirectUrl = sb_url.toString();

                String first = weixinManagerSetting.getTemplateAuditFirst();

                String keynote1 = "审核通过！";
                String templateNo = WeixinUtil.TEMPLATE_MSG_AUDIT_SUCCESS;
                if (weixinManager.getStatus().equals(WeixinManager.WEIXINMANAGER_STATUS_FAILURE)) {
                    keynote1 = "审核未通过";
                    templateNo = WeixinUtil.TEMPLATE_MSG_AUDIT_FAILED;
                }

                String keynote2 = "客户经理";

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String keynote3 = dateFormat.format(new Date());

                String remark = weixinManagerSetting.getTemplateAuditRemark();
                remark += " 备注：" + db.getAuditMemo();

                String errmsg = WeixinUtil
                    .sendTemplateTextMessage(templateNo, wxSetting.getWxAppId(),
                        wxSetting.getWxAppSecret(), db.getWxuser(), redirectUrl, first, keynote1,
                        keynote2, keynote3, remark);
                if (StringUtils.isNotBlank(errmsg)) {
                    result.setErrorcode(errmsg);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            result.setSuccess(true);
            return result;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "orderRecord/{id}", method = RequestMethod.GET)
    public ModelAndView orderRecord(@PathVariable("id") Long id) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("weixin")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("weixinmanager/orderrecord");
            mv.addObject("weixinManager", weixinManagerService.findOne(id));
            mv.addObject("page", weixinManagerXOrderService.queryByManagerId(id));

            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    public @ResponseBody
    Result<Integer> delete(Long id, HttpServletRequest req) {

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("weixin")) {
            Result<Integer> result = new Result<Integer>();

            if (null != id) {
                WeixinManager weixinManager = weixinManagerService.findOne(id);
                if (null != weixinManager) {

                    String detail = "【%s】删除了客户经理【" + weixinManager.getName() + "】";

                    weixinManagerService.delete(weixinManager);

                    OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
                    applicationEventMulticaster.multicastEvent(operationLogEvent);

                    result.setSuccess(true);
                    result.setMessage("删除成功");
                } else {
                    result.setMessage("没有可以删除的数据");
                }
            } else {
                result.setMessage("没有可以删除的数据");
            }

            return result;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "tixian", method = RequestMethod.GET)
    public ModelAndView tixian(String param) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("managertixian")) {
            ModelAndView mv = new ModelAndView();
            mv.setViewName("weixinmanager/tixian");
            mv.addObject("page", weixinManagerTixianRecordService
                .queryAll(null, Pager.getPage(), Pager.getLimit(), null, null, param));

            mv.addObject("param", param);
            return mv;
        } else {
            throw new NoPermissionException();
        }

    }

    @RequestMapping(value = "tixian", method = RequestMethod.POST)
    public synchronized @ResponseBody
    Result<WeixinManagerTixianRecord> tixian(
        HttpServletResponse response, HttpServletRequest request,
        Long id, Integer status) {

        Result<WeixinManagerTixianRecord> vo = new Result<WeixinManagerTixianRecord>();
        vo.setSuccess(false);

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        if (subject.isPermitted("managertixian")) {

            WeixinManagerTixianRecord weixinManagerTixianRecord = weixinManagerTixianRecordService
                .findOne(id);

            if (!weixinManagerTixianRecord.getStatus()
                .equals(WeixinManagerTixianRecord.STATUS_NOTYET)) {
                vo.setMessage("该申请已审核，不能重复审核");
                return vo;
            }

            WeixinManager weixinManager = weixinManagerService
                .findOne(weixinManagerTixianRecord.getManagerId());
            if (status.equals(WeixinManagerTixianRecord.STATUS_OK) && null == weixinManager) {
                vo.setMessage("该客户经理已被删除，不能审核通过");
                return vo;
            }

            if (status.equals(WeixinManagerTixianRecord.STATUS_OK) && null != weixinManager) {
                if (StringUtils.isBlank(weixinManager.getWxuser())) {
                    vo.setMessage("未绑定openId，不能审核通过");
                    return vo;
                }
            }

            if (status.equals(WeixinManagerTixianRecord.STATUS_OK)) {
                weixinManagerTixianRecord.setStatus(WeixinManagerTixianRecord.STATUS_OK);
            } else if (status.equals(WeixinManagerTixianRecord.STATUS_FAILED)) {
                weixinManagerTixianRecord.setStatus(WeixinManagerTixianRecord.STATUS_FAILED);
            }

            vo = weixinManagerTixianRecordService
                .tixianAudit(weixinManagerTixianRecord, weixinManager, response, request);

            if (vo.isSuccess()) {

                Setting setting = settingUtils.get();
                WxSetting wxSetting = settingUtils.getWx();
                WeixinManagerSetting weixinManagerSetting = weixinManagerSettingService.findOne();
                StringBuilder sb_url = new StringBuilder();
                sb_url.append(WeixinUtil.WX_URL).append("/wxmanager/").append(setting.getSecKey())
                    .append("?appId=").append(wxSetting.getWxAppId());
                String redirectUrl = sb_url.toString();

                String first = weixinManagerSetting.getTemplateAuditFirst();

                String keynote1 = "";
                String templateNo = "";

                String keynote2 = "提现申请";

                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String keynote3 = dateFormat.format(new Date());

                String remark = weixinManagerSetting.getTemplateAuditRemark();

                if (status.equals(WeixinManagerTixianRecord.STATUS_OK)) {
                    keynote1 = "已通过！";
                    templateNo = WeixinUtil.TEMPLATE_MSG_AUDIT_SUCCESS;
                    String detail =
                        "【%s】为客户经理【" + weixinManagerTixianRecord.getManagerName() + "】提现成功，金额："
                            + weixinManagerTixianRecord.getMoney() + "";
                    OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
                    applicationEventMulticaster.multicastEvent(operationLogEvent);
                } else if (status.equals(WeixinManagerTixianRecord.STATUS_FAILED)) {
                    keynote1 = "未通过";
                    templateNo = WeixinUtil.TEMPLATE_MSG_AUDIT_FAILED;
                    String detail =
                        "【%s】驳回了客户经理【" + weixinManagerTixianRecord.getManagerName() + "】提现申请，金额："
                            + weixinManagerTixianRecord.getMoney() + "";
                    OperationLogEvent operationLogEvent = new OperationLogEvent(principal, detail);
                    applicationEventMulticaster.multicastEvent(operationLogEvent);
                }

                String errmsg = WeixinUtil
                    .sendTemplateTextMessage(templateNo, wxSetting.getWxAppId(),
                        wxSetting.getWxAppSecret(), weixinManager.getWxuser(), redirectUrl, first,
                        keynote1, keynote2, keynote3, remark);

                vo.setErrorcode(errmsg);
            }

            return vo;
        } else {
            vo.setMessage("您没有该权限！");
            return vo;
        }
    }

    @RequestMapping(value = "countWaitAudit", method = RequestMethod.GET)
    public @ResponseBody
    Result<Long> countWaitAudit() {

        Result<Long> result = new Result<Long>();

        Subject subject = SecurityUtils.getSubject();
        Principal principal = (Principal) subject.getPrincipal();

        Long companyId = null;
        if (!principal.isCompanyType()) {
            companyId = principal.getCompanyId();
        }

        Long count = weixinManagerTixianRecordService.countNewByCompany(companyId);

        result.setSuccess(true);
        result.setResult(count);
        return result;
    }


    @RequestMapping(value = "getAddress", method = RequestMethod.GET)
    public @ResponseBody
    List<Map<String, Object>> getAddress(String city, String keyword) {

        Subject subject = SecurityUtils.getSubject();

        if (subject.isPermitted("weixin")) {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

            try {
                JSONArray placesArray = HttpClientUtil.getPlacesByName(keyword, city);
                if (null != placesArray && placesArray.length() > 0) {
                    for (int i = 0; i < placesArray.length(); i++) {
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("name", placesArray.getJSONObject(i).getString("name"));
                        map.put("lng", placesArray.getJSONObject(i).getJSONObject("location")
                            .getDouble("lng"));
                        map.put("lat", placesArray.getJSONObject(i).getJSONObject("location")
                            .getDouble("lat"));
                        list.add(map);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return list;
        } else {
            throw new NoPermissionException();
        }
    }

    @RequestMapping(value = "findByCompany", method = RequestMethod.GET)
    public @ResponseBody
    List<WeixinManager> findByCompany(Long companyId) {

        List<WeixinManager> weixinManagers = weixinManagerService.findByCompany(companyId);

        return weixinManagers;

    }

    @RequestMapping(value = "findOKByCompany", method = RequestMethod.GET)
    public @ResponseBody
    List<WeixinManager> findOKByCompany(Long companyId) {

        List<WeixinManager> weixinManagers = weixinManagerService.findOKByCompany(companyId);

        return weixinManagers;

    }

    private boolean phoneExists(String phone, Long id) {
        WeixinManager wm = weixinManagerService.findByPhone(phone);

        if (null != wm) {

            if (null != id && id.equals(wm.getId())) {

                return false;

            }

            return true;
        }

        return false;
    }

    private boolean idCardExists(String idCard, Long id) {
        WeixinManager wm = weixinManagerService.findByIdCard(idCard);

        if (null != wm) {

            if (null != id && id.equals(wm.getId())) {

                return false;

            }

            return true;
        }

        return false;
    }

    @org.springframework.web.bind.annotation.InitBinder
    public void InitBinder(WebDataBinder dataBinder) {

        SimpleDateFormat dateFormat = com.ybkj.daijia.constant.DateConstant.DATE_TIME;
        dateFormat.setLenient(false);
        dataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

    }
}